/**
 * AI创作工坊核心数据类型定义
 *
 * 本文件包含创作工坊功能的所有核心接口和类型定义
 * @module types/brainstorm
 */

// ==================== 会话相关 ====================

/**
 * 创作会话元数据
 */
export interface SessionMetadata {
  targetAudience?: string;
  targetWordCount?: number;
  style?: 'academic' | 'news' | 'story' | 'marketing' | 'tutorial' | 'essay';
  tone?: 'formal' | 'casual' | 'humorous' | 'professional';
  tags?: string[];
}

/**
 * 创作会话
 */
export interface CreationSession {
  id: string;
  title: string;
  topic: string;
  hotTopicId?: string;
  templateId?: string;
  stage: 'brainstorming' | 'writing' | 'optimizing' | 'completed';

  questions: Question[];
  viewpoints: Viewpoint[];
  mindMap?: MindMapData;
  outline?: Outline;
  paragraphs: ParagraphData[];

  metadata: SessionMetadata;

  createdAt: string;
  updatedAt: string;
}

// ==================== 问答相关 ====================

/**
 * 问题类型
 */
export type QuestionPurpose = 'audience' | 'viewpoint' | 'material' | 'style' | 'goal';

/**
 * 问答项
 */
export interface Question {
  id: string;
  text: string;
  purpose: QuestionPurpose;
  explanation: string;
  hints?: string[];      // 提示要点
  examples?: string[];   // 参考示例
  tips?: string[];      // 写作技巧
  answer?: string;
  skipped: boolean;
  order: number;
}

// ==================== 观点相关 ====================

/**
 * 观点视角类型
 */
export type PerspectiveType = 'rational' | 'emotional' | 'innovative' | 'popular';

/**
 * 观点评分详情
 */
export interface ScoreDetails {
  relevance: number;      // 相关性 0-100
  novelty: number;        // 新颖性 0-100
  feasibility: number;    // 可行性 0-100
  impact: number;         // 影响力 0-100
}

/**
 * 观点
 */
export interface Viewpoint {
  id: string;
  model: string;          // AI模型名称: 'gpt-4' | 'claude' | 'deepseek' | 'qwen'
  perspective: PerspectiveType;

  // 区分主题描述和核心观点
  topicDescription?: string;    // 原始主题描述（用户输入的长文本）
  coreViewpoint: string;         // 简短核心观点（1-2句话，由AI生成或从主题中提取）

  arguments: string[];
  scenario: string;
  expectedEffect: string;

  generating: boolean;
  completed: boolean;
  error?: string;
  content?: string;       // 流式生成时的临时内容

  aiScore: number;        // 综合评分 0-100
  scoreDetails: ScoreDetails;
  scoreReason?: string;

  createdAt: string;
}

// ==================== 思维导图相关 ====================

/**
 * 思维导图节点类型
 */
export type MindMapNodeType = 'topic' | 'question' | 'viewpoint' | 'argument';

/**
 * 思维导图节点
 */
export interface MindMapNode {
  id: string;
  type: MindMapNodeType;
  label: string;
  content: string;
  perspective?: PerspectiveType;
  position: { x: number; y: number };
  style?: Record<string, any>;
}

/**
 * 思维导图连线
 */
export interface MindMapEdge {
  id: string;
  source: string;
  target: string;
  label?: string;
  style?: Record<string, any>;
}

/**
 * 思维导图数据
 */
export interface MindMapData {
  nodes: MindMapNode[];
  edges: MindMapEdge[];
}

// ==================== 提纲相关 ====================

/**
 * 提纲风格
 */
export type OutlineStyle = 'academic' | 'news' | 'story' | 'marketing' | 'tutorial';

/**
 * 提纲节点状态
 */
export type OutlineNodeStatus = 'pending' | 'in_progress' | 'completed';

/**
 * 提纲节点
 */
export interface OutlineNode {
  id: string;
  title: string;
  level: number;          // 层级: 1-主标题, 2-一级标题, 3-二级标题
  order: number;
  children?: OutlineNode[];

  mainPoints?: string[];  // 主要论点
  keywords?: string[];
  references?: string[];  // 参考资料
  notes?: string;

  estimatedWords: number;
  status: OutlineNodeStatus;
}

/**
 * 提纲
 */
export interface Outline {
  id: string;
  sessionId: string;
  titleSuggestions: string[];
  nodes: OutlineNode[];
  style: OutlineStyle;
  totalEstimatedWords: number;

  createdAt: string;
  updatedAt: string;
}

/**
 * 提纲优化建议
 */
export interface OptimizationSuggestion {
  type: 'logic' | 'balance' | 'duplication' | 'missing' | 'order';
  severity: 'high' | 'medium' | 'low';
  nodeId?: string;
  message: string;
  suggestion: string;
  autoFixAvailable: boolean;
}

// ==================== 段落相关 ====================

/**
 * 段落生成策略
 */
export type ParagraphStrategy = 'concise' | 'detailed' | 'vivid' | 'data-driven' | 'mixed';

/**
 * 段落评分指标
 */
export interface ParagraphMetrics {
  relevance: number;      // 相关性 0-100
  readability: number;    // 可读性 0-100
  originality: number;    // 原创度 0-100
  coherence: number;      // 连贯性 0-100
}

/**
 * 段落版本
 */
export interface ParagraphVersion {
  content: string;
  strategy: ParagraphStrategy;
  generating: boolean;
  error?: string;

  aiScore: number;
  wordCount: number;
  metrics: ParagraphMetrics;
  scoreReason?: string;
}

/**
 * 段落数据
 */
export interface ParagraphData {
  id: string;
  outlineNodeId: string;
  versions: ParagraphVersion[];
  selectedVersionIndex: number;

  createdAt: string;
  updatedAt: string;
}

// ==================== 模板相关 ====================

/**
 * 文章模板变量
 */
export interface TemplateVariable {
  name: string;
  description: string;
  defaultValue?: string;
  required: boolean;
}

/**
 * 文章模板
 */
export interface ArticleTemplate {
  id: string;
  name: string;
  description: string;
  type: 'tutorial' | 'review' | 'news' | 'academic' | 'marketing' | 'essay';

  questions: Omit<Question, 'id' | 'answer' | 'skipped'>[];
  outline: Omit<OutlineNode, 'id' | 'status'>[];
  variables: TemplateVariable[];

  tags: string[];
  isOfficial: boolean;
  usageCount: number;
  rating: number;

  authorId: string;
  createdAt: string;
  updatedAt: string;
}

// ==================== 灵感库相关 ====================

/**
 * 灵感素材类型
 */
export type InspirationType = 'text' | 'link' | 'quote' | 'image' | 'note';

/**
 * 灵感素材
 */
export interface InspirationItem {
  id: string;
  type: InspirationType;
  title: string;
  content: string;
  url?: string;
  imageUrl?: string;

  tags: string[];
  source?: string;

  aiGenerated: boolean;
  aiSummary?: string;
  aiKeywords?: string[];
  qualityScore?: number;

  createdAt: string;
  updatedAt: string;
}

// ==================== Copilot相关 ====================

/**
 * Copilot建议类型
 */
export type SuggestionType =
  | 'add_example'
  | 'add_data'
  | 'add_transition'
  | 'expand'
  | 'simplify'
  | 'fix_grammar'
  | 'improve_clarity';

/**
 * Copilot建议
 */
export interface CopilotSuggestion {
  id: string;
  type: SuggestionType;
  title: string;
  description: string;
  action: string;
  targetText?: string;
  targetPosition?: number;

  priority: 'high' | 'medium' | 'low';
  autoApplicable: boolean;

  createdAt: string;
}

/**
 * Copilot对话消息
 */
export interface ChatMessage {
  id: string;
  role: 'user' | 'assistant';
  content: string;
  timestamp: string;
}

// ==================== 质量分析相关 ====================

/**
 * 质量问题
 */
export interface QualityIssue {
  id: string;
  type: 'structure' | 'logic' | 'grammar' | 'readability' | 'originality';
  severity: 'error' | 'warning' | 'info';
  message: string;
  position?: { start: number; end: number };
  suggestion?: string;
}

/**
 * SEO关键词
 */
export interface SEOKeyword {
  word: string;
  count: number;
  density: number;      // 密度百分比
  positions: number[];
}

/**
 * SEO数据
 */
export interface SEOData {
  keywords: SEOKeyword[];
  titleScore: number;
  metaDescription?: string;
  headingStructure: Array<{ level: string; text: string }>;
  recommendations: string[];
}

/**
 * 文章优化报告
 */
export interface OptimizationReport {
  overallScore: number;
  structureScore: number;
  coherenceScore: number;
  readabilityScore: number;
  originalityScore: number;
  seoScore: number;

  suggestions: OptimizationSuggestion[];
  issues: QualityIssue[];
}

// ==================== 统计相关 ====================

/**
 * 成就
 */
export interface Achievement {
  id: string;
  name: string;
  description: string;
  icon: string;
  category: 'beginner' | 'milestone' | 'quality' | 'efficiency' | 'exploration' | 'community';

  unlocked: boolean;
  progress?: number;
  total?: number;
  unlockedAt?: string;
}

/**
 * 用户等级
 */
export interface UserLevel {
  level: number;
  currentXP: number;
  nextLevelXP: number;
  privileges: string[];
}

/**
 * 用户统计数据
 */
export interface UserStatistics {
  totalWords: number;
  totalArticles: number;
  totalDays: number;
  consecutiveDays: number;
  avgQualityScore: number;
  aiUsageCount: number;
  templateUsageCount: number;
  inspirationCount: number;
}

/**
 * 效率分析数据
 */
export interface EfficiencyAnalysis {
  avgTimePerArticle: number;
  stageBreakdown: {
    brainstorming: number;
    writing: number;
    optimizing: number;
  };
  peakHours: number[];
  efficiencyTrend: Array<{ date: string; efficiency: number }>;
}

/**
 * 质量趋势数据
 */
export interface QualityTrendData {
  date: string;
  score: number;
  breakdown: {
    structure: number;
    coherence: number;
    readability: number;
    originality: number;
  };
}

/**
 * 创作日历数据
 */
export interface CreationCalendarData {
  [date: string]: {
    wordCount: number;
    articleCount: number;
  };
}

/**
 * 排行榜数据
 */
export interface RankingData {
  rankings: Array<{
    userId: string;
    username: string;
    value: number;
    rank: number;
  }>;
  myRank: number;
}

// 所有类型已通过 export interface 和 export type 导出
// 无需重复导出
